// xc_lshop/distr/manage/orderlist/arealistorder.js

var actbill = require('../../model/distribution_manage.js')
var actteambill = require('../../model/distribution_team.js')

Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    nav:[],
    areaid:"0",
    areaname:"",
    area:[],
    status:0,
    orderstatus:
    [
      {name:"全部",status:0},
      {name:"未付款",status:-1},
      {name:"未分佣",status:1},
      {name:"已分佣",status:2},
    ],
    statusinfo:{
      "-1":{
        name:"未付款",
        color:"#FE0000"
      },
      "-1":{
        name:"未分佣",
        color:"#FE9000"
      },
      "-1":{
        name:"已分佣",
        color:"#0FAB0A"
      },
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
   

  },
 
  xcloaddata: function (options, config) {
    options.areaname=decodeURIComponent(options.areaname);
    this.setData({
      options:options
    })
   this.gettimlist(1)
   this.getarea()
  },
  getpagelist(rest, strwhere) {
    var that = this
    if (rest == 1) {
      that.setData({
        modellist: []
      });
      if (!strwhere) {
        strwhere = {}
        if(this.data.status){
          strwhere["status"] = this.data.status
        }
        if(this.data.areaid){
          strwhere["areaname"] = this.data.areaname
        }
        if(this.data.curid){
          strwhere["date"] = this.data.curid
        }
      }
    }
    this.setData({
      isload: true
    })
    actteambill.areaorderlistoffset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
        if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      wx.stopPullDownRefresh();
      updatemdoel["isload"] = false
      that.setData(updatemdoel);
    })
    return;
  },
  onlicknav(e){
    var id=e.currentTarget.dataset.id
    this.setData({
      curid:id
    })
    this.getpagelist(1)
 },
  onChangestatus(e){
    this.setData({
      status:e.detail.name
    })
    this.getpagelist(1)

  },
  onareaclick(e){
    var dataset=e.currentTarget.dataset;
    var update={}
    if(dataset.id){
      update['areaid']=dataset.id;
      update['areaname']=""
    }else{
      var item=dataset.item;
      update['areaid']=item.id;
      if(item.regionauth==1){
        update['areaname']=item.province
      }else if(item.regionauth==2){
        update['areaname']=item.city
      }else if(item.regionauth==3){
        update['areaname']=item.county
      }
    }
    this.setData(update)
    this.getpagelist(1)

  },
  getarea(){
    var that = this
    actbill.getarea().then(function (res) {
        console.log("res")
        console.log(res)
        if(res['status']==1){
          that.setData({
            area:res['obj']
          })
          if(that.data.options.areaname){
            that.changearea(res['obj'],that.data.options.areaname)
          }
        }
    })
  },
  changearea(areas,areaname){
    var areaid=0
    for(var i in areas){
      var name=""
      if(areas[i]['regionauth']==1){
        name=areas[i]['province']
      }else if(areas[i]['regionauth']==2){
        name=areas[i]['city']
      }else if(areas[i]['regionauth']==3){
        name=areas[i]['county']
      }
      if(name==areaname){
        areaid=areas[i]['id'];
        break;
      }
    }
    this.setData({
      areaid:areaid,
      areaname:areaname
    })
  },
  loadinglist(){
    var page= this.data.page + 1;
    var navdata= this.data.navdata;
    console.log(navdata)
    this.gettimlist(page,5,navdata)
  },
  gettimlist(page,limit=5,data=null){
    var date=new Date();
    var nowmonth="";
    var nowyear="";
    if(page==1){
      nowmonth=date.getMonth()+1;
      nowyear=date.getFullYear();
    }else{
      nowmonth=data['month'];
      nowyear=data['year'];
    }
    var list=[]
    for(var i=0;i<limit;i++){
      
      var item={}
      item['name']=nowyear+"年"+nowmonth+"月";
      item['id']=nowyear+"-"+ (nowmonth<10?'0'+nowmonth:nowmonth)
      list.push(item)

      nowmonth=nowmonth - 1;
      if(nowmonth==0){
        nowmonth=12;
        nowyear= nowyear - 1
      }
    }
    var updata={ }
    updata["page"]=page;
    updata["nav"]=this.data.nav.concat(list);
    updata["navdata"]={month:nowmonth,year:nowyear};

    if(this.data.options.curid){
      updata["curid"]=this.data.options.curid;
    }else
    if(page==1){
      updata["curid"]=list[0]['id'];
    }
    this.setData(updata)
    if(page==1){
      this.getpagelist(1)
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getpagelist(-1)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})